package home.control.logicElements;

import home.control.R;
import home.control.data.FactoryData;
import home.control.data.IGetData;

import java.util.List;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class InstantActionAdapter extends BaseAdapter implements OnCheckedChangeListener {
	
	private Context context;
	private List<HomeParts> instantActions;
	private IGetData mock;
	private String TAG = "InstantActionAdapter";
	
	public InstantActionAdapter(Context context, IGetData mock){
		this.instantActions = mock.getInstantActions();
		this.context=context;
		this.mock = mock;
	}
	
	@Override
	public int getCount() {
		return instantActions.size();
	}

	@Override
	public Object getItem(int position) {
		return instantActions.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		HomeParts homePart = instantActions.get(position);
		Log.v(TAG,"Home part = " + homePart.name() + " position  = " + position);
		
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.actionrow, null);
		}
		TextView text = (TextView) convertView.findViewById(R.id.actionNameTV);
		text.setText(homePart.toString());
		
		CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.actionIsSelectedCB);
		//checkBox.setChecked(mock.checkStatus(homePart));
		//new CheckStatus().execute(homePart, checkBox);
		checkBox.setChecked(mock.checkStatus(homePart));
		
		checkBox.setOnCheckedChangeListener(this);
		checkBox.setTag(homePart);
		
		return convertView;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		HomeParts homePart = (HomeParts) buttonView.getTag();
		Log.v("onCheckedChanged","Home part = " + homePart.name());
		
		new SetStatus().execute(homePart,isChecked);
		String msg;
		if(isChecked){
			msg= ": turn on";
		}else{
			msg =": turn off";
		}
		Toast.makeText(context, homePart + msg, Toast.LENGTH_SHORT).show();
	}
	
//	/**Async task to checkStatus in ControlPanel */
//	private class CheckStatus extends AsyncTask<Object, Void, CheckBox>{
//		private boolean status;
//		@Override
//		protected CheckBox  doInBackground(Object... params) {
//			HomeParts homePart = (HomeParts)params[0];
//			status = mock.checkStatus(homePart);
//			return (CheckBox)params[1];
//		}
//		
//		@Override
//		protected void onPostExecute(CheckBox result) {
//			result.setChecked(status);
//		}	
//	}
//	
	/**Async task to setStatus in ControlPanel */
	private class SetStatus extends AsyncTask<Object, Void, Void>{
		@Override
		protected Void  doInBackground(Object... params) {
			HomeParts homePart = (HomeParts)params[0];
			Boolean value = (Boolean)params[1];
			Log.d(TAG, "Zmieniono home parta: " + homePart);
			mock.setStatus(homePart,value);
			return null;
		}
	}

}
